#ifndef SANDBOX_H
#define SANDBOX_H

#include "SmallEngine.h"
// #include "ScriptCommon.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

typedef struct
{
    CC::Scene* s;
    CC::Layer* l;
}ThreadInfoData;

class Sandbox : public CC::Layer
{
public:
	Sandbox(const std::string &name = "Sandbox", CC::Scene* scene = nullptr);
	virtual ~Sandbox();

	virtual void OnAttach() override;
	virtual void OnDetach() override;

    virtual void OnUpdate(CC::Timestep ts, std::string renderscene = "") override;
	virtual void OnImGuiRender() override;
    virtual void OnEvent(CC::Event &e) override;
    CC::Scene* GetScene();
    float GetWidth();
    float GetHeight();
    CC::Entity* GetLight() {
        return light;
    }

    void CreateButton(glm::vec3 pos, glm::vec3 scale, std::wstring text, std::function<void()> callback);

    template <typename T>
    void CreateBackBtn(glm::vec3 pos, std::wstring text, CC::Layer* currentLayer, std::string title) {
        CreateButton(pos, { 200.0f, 50.0f, 1.0f }, text, [=]() {
            CC::Application::Get().SubmitToMainThread(CC::ThreadInfo([=](void* param) {
                CC::Application::Get().PopLayer(currentLayer);
                delete currentLayer;
                }, nullptr));

            CC::Scene* s = new CC::Scene();
            s->LoadFont(PATHCONFIG("assets/fonts/Alimama_DongFangDaKai_Regular.ttf"), "Alimama_DongFangDaKai_Regular");

            CC::Application::Get().SetWindowTitle(title);

            CC::Application::Get().SubmitToMainThread(CC::ThreadInfo([=](void* param) {
                CC::Application::Get().PushLayer((CC::Layer*)new T(title, (CC::Scene*)(param)));
                }, s));
            });
    }
    
protected:
	virtual void OnWindowResize(CC::WindowResizeEvent &e);
	virtual void OnMouseButtonPressed(CC::MouseButtonPressedEvent &e);
	virtual void OnMouseButtonReleased(CC::MouseButtonReleasedEvent &e);
	virtual void OnMouseButtonMoved(CC::MouseMovedEvent &e);
    virtual void OnMouseScrolled(CC::MouseScrolledEvent &e);
	virtual void OnKeyPressed(CC::KeyPressedEvent &e);
	virtual void OnKeyReleased(CC::KeyReleasedEvent &e);
	virtual void OnKeyWinPressed(CC::KeyWinEvent &e);
	virtual void AddEntityScriptRecursively(CC::Entity *parent);

    virtual void ShowProjectTreeTop();
    virtual void ShowProjectTree(CC::Entity *parent);
    virtual void ShowProperty();

    virtual void HandleLeftButtonPressed();
    virtual void HandleLeftButtonDouble();
    virtual void HandleLeftButtonMoved();
    virtual void HandleLeftButtonReleased();

    virtual void HandleMidButtonPressed();
    virtual void HandleMidButtonMoved();
    virtual void HandleMidButtonReleased();

    virtual void HandleRightButtonPressed();
    virtual void HandleRightButtonMoved();
    virtual void HandleRightButtonReleased();

    virtual void HandleMouseMoved();

private:

	glm::vec4 m_SquareColor = {0.2f, 0.3f, 0.8f, 1.0f};
	glm::vec4 m_FontColor = {0.0f, 0.0f, 0.0f, 1.0f};
	glm::vec4 m_ClearColor = {0.0f, 0.0f, 0.0f, 0.0f};

	CC::Scene* m_Scene;
    CC::Entity* m_CurrentEntity;
    CC::Entity* light;

	float m_MouseX = 0.0;
	float m_MouseY = 0.0;
	
    float m_LastMouseX = 0.0;
    float m_LastMouseY = 0.0;

    float m_LastDoubleMouseX = 0.0;
    float m_LastDoubleMouseY = 0.0;

    bool m_ButtonLeftPressed = false;    
    bool m_ButtonRightPressed = false;
    bool m_ButtonMidPressed = false;

    int m_ButtonLeftDouble = 0;
    float m_ButtonLeftDoubleTimeMax = 0.3;//s
    float m_ButtonLeftDoubleStartTime = 0.0;

    float m_AutoSaveTimeMax = 5.0;//s

    bool m_UseEditCamera = false;

    float m_CameraMoveSpeed = 0.1;
    float m_CameraScrollSpeed = 0.1;
    bool m_IsFirstPerson = false;


};

#endif